package org.testis.utils.middleware;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 
 * @author nurhat
 */
public class HashMapUtils {

    public static LinkedHashMap sortHashMapByKeys(HashMap passedMap) {
	if (passedMap == null) {
	    return null;
	}
	List mapKeys = new ArrayList(passedMap.keySet());
	Collections.sort(mapKeys);

	LinkedHashMap sortedMap = new LinkedHashMap();

	Iterator keyIt = mapKeys.iterator();
	while (keyIt.hasNext()) {
	    Object key = keyIt.next();
	    Object val = passedMap.get(key);
	    sortedMap.put(key, val);
	}
	return sortedMap;
    }

    public static LinkedHashMap sortHashMapByValues(HashMap passedMap) {
	if (passedMap == null) {
	    return null;
	}
	List mapKeys = new ArrayList(passedMap.keySet());
	List mapValues = new ArrayList(passedMap.values());
	Collections.sort(mapValues);
	Collections.sort(mapKeys);

	LinkedHashMap sortedMap = new LinkedHashMap();

	Iterator valueIt = mapValues.iterator();
	while (valueIt.hasNext()) {
	    Object val = valueIt.next();
	    Iterator keyIt = mapKeys.iterator();

	    while (keyIt.hasNext()) {
		Object key = keyIt.next();
		String comp1 = passedMap.get(key).toString();
		String comp2 = val.toString();

		if (comp1.equals(comp2)) {
		    passedMap.remove(key);
		    mapKeys.remove(key);
		    sortedMap.put(key, val);
		    break;
		}

	    }

	}
	return sortedMap;
    }
}
